package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;


    @GetMapping()
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        // 调用业务层分页查询功能
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        // 响应
        return Result.success(pageBean);
    }

    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("添加信息");
        empService.addEmp(emp);
        return Result.success();
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("修改信息");
        empService.updateEmp(emp);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteEmp(@PathVariable Integer id) {
        log.info("删除信息");
        empService.delete(id);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询");
        Emp byId = empService.findById(id);
        return Result.success(byId);

    }
}
